package com.xwqiang.handle;

import com.xwqiang.utils.AppExceptionCodeMsg;
import com.xwqiang.utils.Resp;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandle {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public  <T> Resp<T> handleException(Exception e) {
        System.out.println(e);
        //如果是自定义异常,转换格式
        if(e instanceof AppException){
            return Resp.error(10000,e.getMessage());
        }

        if(e instanceof AccessDeniedException){
            return Resp.error(AppExceptionCodeMsg.FORBIDDEN_ERR);
        }
        return Resp.error(AppExceptionCodeMsg.SYSTERM_ERR);
    }


}
